package com.lkj.youmeng.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

public class StaggeredItemDecoration extends DividerItemDecoration {
    private int padding;
    private Drawable mDivider;
    private static final int[] ATTRS = new int[]{ android.R.attr.listDivider };
    public StaggeredItemDecoration(Context context,int padding) {
        super(context, VERTICAL);
        this.padding = padding;
        TypedArray a = context.obtainStyledAttributes(ATTRS);
        this.mDivider = a.getDrawable(0);
        if (this.mDivider == null) {
            Log.w("aaa", "@android:attr/listDivider was not set in the theme used for this DividerItemDecoration. Please set that attribute all call setDrawable()");
        }

        a.recycle();
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.left = padding;
        outRect.top = padding;
        outRect.right = padding;
        outRect.bottom = padding;
//        outRect.set(0, 0, 0, 0);

    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
//        drawVertical(c,parent);
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getLeft() + params.leftMargin;
            final int right = child.getRight()+params.rightMargin;
            Log.e("aaa","left =="+left+",top =="+top+",right =="+right+",bottom == "+bottom);
            getDrawable().setBounds(left, top, right, bottom);
            getDrawable().draw(c);
        }
    }
}
